package dao.impl;

import dao.UserDao;
import entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
import rowmapper.UserRowMapper;

import java.util.List;

public class UserDaoImpl implements UserDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void insertUser(User user) {
        String sql = new StringBuffer()
                .append(" insert into ")
                .append(" t_user ")
                .append("   (username,password,phone,address)")
                .append(" values ")
                .append("   (?,?,?,?)")
                .toString();
        jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getPhone(),user.getAddress());
    }

    @Override
    public void deleteById(Integer id) {
        String sql = new StringBuffer()
                .append("delete ")
                .append(" from ")
                .append("t_user ")
                .append(" where")
                .append( "id = " )
                .append(" ? ")
                .toString();
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateUser(User user) {

    }

    @Override
    public User selectById(Integer id) {
        String sql = new StringBuffer()
                .append(" select id,username,password,phone,address")
                .append(" from t_user ")
                .append(" where id = ?")
                .toString();
        List<User> users = jdbcTemplate.query(sql,new Object[]{id}, new UserRowMapper());
        return users.isEmpty()?null:users.get(0);
    }

    @Override
    public List<User> selectAll() {
        return null;
    }

    @Override
    public Long insertReturnPrimaryKey(User user) {
        return null;
    }
}
